package reflect;

import reflect.annotation.AutoRunClass;
import reflect.annotation.AutoRunMethod;

import java.io.File;
import java.lang.reflect.Method;

public class Test5 {
    public static void main(String[] args) throws Exception {
        File dir = new File(Test4.class.getResource(".").toURI());
        File packageName = new File(Test4.class.getPackage().getName());
        File[] files = dir.listFiles(f -> f.getName().endsWith(".class"));
        for (File file : files) {
            String className = file.getName().split("\\.")[0];
            Class<?> clazz = Class.forName(packageName + "." + className);
            if (clazz.isAnnotationPresent(AutoRunClass.class)) {
                Object obj = clazz.newInstance();
                Method[] declaredMethods = clazz.getDeclaredMethods();
                for (Method declaredMethod : declaredMethods) {
                    if (declaredMethod.isAnnotationPresent(AutoRunMethod.class)) {
                        int value = declaredMethod.getAnnotation(AutoRunMethod.class).value();
                        System.out.println(declaredMethod.getName() + "被执行" + value + "次...");
                    }
                }
            }
        }
    }
}
